#include "main.h"

/******************init class & param code begin******************/

WHT20 wht20;
AD7193 ad7193;
MultiTaskModel model;

float Voltage_scaled[8];
/******************init class & param code end******************/

/**********************function code begin***********************/

#if LV_USE_LOG != 0
/* Serial debugging */
void my_print(const char * buf)
{
    Serial.printf(buf);
    Serial.flush();
}
#endif
/***********************function code end*************************/

void setup()
{
  Serial.begin(115200);

  /******************3V3A供电引脚开启*******************/
  pinMode(AD7193_EN_PIN, OUTPUT);
  digitalWrite(AD7193_EN_PIN, HIGH);

  /*****************泵控制引脚拉低（关闭）***************/
  pinMode(PUMP_SW_PIN, OUTPUT);
  digitalWrite(PUMP_SW_PIN, LOW);

  /*********************WHT20/I2C初始化******************/
  Wire.begin(SDA, SCL);

  /************************LCD屏幕***********************/
  lv_init();
	user_init();
#if LV_USE_LOG != 0
  lv_log_register_print_cb( my_print );
#endif

  /******************AD7193/SPI初始化******************/
  ad7193.begin();
  ad7193.SetPGAGain(1);
  ad7193.reset();
  delayMicroseconds(500);
  ad7193.AppendStatusValuetoData();
  ad7193.setSinc(0); 
  ad7193.SetAverage(16);
  ad7193.SetChop(1); 
  ad7193.SetRegisterValue(5, 0x40,1, 1);
  ad7193.SetPsuedoDifferentialInputs();
  ad7193.CalibrateFullScale();
  ad7193.ReadRegisterMap();

  /******************init model******************/
  // input.shape must be equal to (num_feature, time_step)
	init_variables();
}

void loop()
{    
  /******************WHT20+AD7193**********************/
  unsigned long Data[8];

  for (int i = 0; i < 8; i++)
  {
    Data[i] = ad7193.ReadADCChannel(i) >> 8;
    Voltage[i] = ad7193.DataToVoltage(Data[i]);
    Voltage_scaled[i] = (Voltage[i] - X_scaler_mean[i]) / X_scaler_scale[i];
  }

  temperature = wht20.getTemperature();
  humidity = wht20.getHumidity();

/******************update sliding window & forward code begin******************/

	sliding_window();
  model_forward();
/********************update sliding window & forward code end******************/

/******************screen display code begin******************/

  update_chart();
  lv_timer_handler();
/******************screen display code end******************/

}
